package com.example.myproject.service.impl;

import com.example.myproject.entity.UserEntity;
import com.example.myproject.mapper.UserMapper;
import com.example.myproject.service.UserService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserEntity> findAll() {
        return userMapper.getAll();
    }

    @Override
    public UserEntity findOneById(Long id) {
        return userMapper.getOneById(id);
    }

    @Override
    public UserEntity findOneByCardNo(String cardNo) {
        return userMapper.getOne(cardNo);
    }

    @Override
    public void save(UserEntity user) {
        userMapper.insert(user);
    }

    @Override
    public void saveAll(List<UserEntity> userEntityList) {
        //...
        if(null == userEntityList || userEntityList.isEmpty()) {
            logger.info("数据记录条目不能为空");
            return;
        }

        logger.info("----------开始记录----------数据条目:{}", userEntityList.size());
        // 查询重复数据
        List<UserEntity> userEntityInDb = Lists.newArrayList();
        for(UserEntity userEntity: userEntityList) {
            UserEntity ue = userMapper.getOneById(userEntity.getId());
            //UserEntity ue = userMapper.getOne(userEntity.getCardNo());
            if(null != ue) {
                userEntityInDb.add(ue);
            }
        }
        // 去掉重复数据
        userEntityList.removeAll(userEntityInDb);

        for(UserEntity userEntity : userEntityList) {
            userMapper.insert(userEntity);
        }

        logger.info("----------记录结束----------数据条目:{}", userEntityList.size());
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void deleteByCardNo(String cardNo) {
        userMapper.deleteByCardNo(cardNo);
    }

    @Override
    public void delete(UserEntity user) {
        //userMapper.deleteById(user.getId());
        userMapper.deleteByCardNo(user.getCardNo());
    }

    @Override
    public void deleteAll() {
        //...
        List<UserEntity> userEntityList = findAll();
        if(userEntityList != null && 0 != userEntityList.size()) {
            for(UserEntity userEntity : userEntityList) {
                //delete(userEntity);
                userMapper.deleteById(userEntity.getId());
                //userMapper.deleteByCardNo(userEntity.getCardNo());
            }
        } else {
            logger.info("表中没有数据，无法删除");
        }
    }

    @Override
    public void update(UserEntity user) {
        userMapper.update(user);
    }

    @Override
    public void updateNoNull(UserEntity user) {
        //
        UserEntity ue = userMapper.getOne(user.getCardNo());
        if(null == user.getCardNo() || user.getCardNo().equals("")) {
            user.setCardNo(ue.getCardNo());
        }
        if(null == user.getUserName() || user.getUserName().equals("")) {
            user.setUserName(ue.getUserName());
        }
        if(null == user.getPassWord() || user.getPassWord().equals("")) {
            user.setPassWord(ue.getPassWord());
        }
        if(null == user.getUserSex() || user.getUserSex().equals("")) {
            user.setUserSex(ue.getUserSex());
        }
        if(null == user.getNickName() || user.getNickName().equals("")) {
            user.setNickName(ue.getNickName());
        }

        userMapper.update(ue);
    }

    @Override
    public void updateAll(List<UserEntity> userEntityList) {
        //
        if(null != userEntityList && 0 != userEntityList.size()) {
            for(UserEntity userEntity : userEntityList) {
                userMapper.update(userEntity);
            }
        } else {
            logger.info("输入参数为空，不能更新");
        }
    }
}
